/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the GLQuake source code.

 The GLQuake source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The GLQuake source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the GLQuake source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// sg_item.cpp - items the client can pickup
//


#include "sg_local.h"


/*
 ==============================================================================

 BSP Items:

 Some items here can look strange because the MDL format do not have support
 for animated textures which some items required. Id Software solved this
 problem by making them into a BSP.
 
 So we load a BSP file instaid of a regular .mdl file (cheap bastards :P).

 ==============================================================================
*/


/*
 ==============================================================================

 ITEM_HEALTH

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemHealth_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================

 FIXME: load with glqRenderer->RegisterModel or glqRenderer->LoadMap?
 ==================
*/
void SG_ItemHealth_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/health");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemHealth_Touch;
}


/*
 ==============================================================================

 ITEM_ARMOR1

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArmor1_Touch (etEntity *self, etEntity *other, const trace_t *trace){

	// TODO: check if the client is valid

	// TODO: set the item value to the clients armor inventroy value
}

/*
 ==================
 
 ==================
*/
void SG_ItemArmor1_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/armor");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArmor1_Touch;
}


/*
 ==============================================================================

 ITEM_ARMOR2

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArmor2_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArmor2_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/armor");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArmor2_Touch;
}


/*
 ==============================================================================

 ITEM_ARMORINV

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArmorInv_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArmorInv_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/armor");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArmorInv_Touch;
}


/*
 ==============================================================================

 ITEM_SHELLS

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemShells_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemShells_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/ammo");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemShells_Touch;
}


/*
 ==============================================================================

 ITEM_SPIKES

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemSpikes_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemSpikes_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/ammo");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemSpikes_Touch;
}


/*
 ==============================================================================

 ITEM_ROCKETS

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemRockets_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemRockets_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/ammo");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemRockets_Touch;
}


/*
 ==============================================================================

 ITEM_CELLS

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemCells_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemCells_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/ammo");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemCells_Touch;
}


/*
 ==============================================================================

 ITEM_KEY1

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemKey1_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemKey1_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/runeKey");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemKey1_Touch;
}


/*
 ==============================================================================

 ITEM_KEY2

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemKey2_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemKey2_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/runeKey");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemKey2_Touch;
}


/*
 ==============================================================================

 ITEM_SIGIL

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemSigil_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemSigil_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/sigil");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemSigil_Touch;
}


/*
 ==============================================================================

 ITEM_ARTIFACT_INVULNERABILITY

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArtifactInvulnerability_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArtifactInvulnerability_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/invulnerability");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArtifactInvulnerability_Touch;
}


/*
 ==============================================================================

 ITEM_ARTIFACT_ENVIROSUIT

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArtifactEnvirosuit_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArtifactEnvirosuit_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/envirosuit");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArtifactEnvirosuit_Touch;
}


/*
 ==============================================================================

 ITEM_ARTIFACT_INVISIBILITY

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArtifactInvisibility_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArtifactInvisibility_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/invisibility");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArtifactInvisibility_Touch;
}


/*
 ==============================================================================

 ITEM_ARTIFACT_SUPER_DAMAGE

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemArtifactSuperDamage_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_ItemArtifactSuperDamage_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/superDamage");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemArtifactSuperDamage_Touch;
}


/*
 ==============================================================================

 ITEM_BACKPACK

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_ItemBackpack_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================

 This function is a little different then the rest because it is beeing
 used when a client dies and not in the entity string like the rest
 ==================
*/
void SG_ItemBackpack_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("items/ammo");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_ItemBackpack_Touch;
}


/*
 ==============================================================================

 WEAPON_SUPERSHOTGUN

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponSuperShotgun_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponSuperShotgun_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/superShotgun");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponSuperShotgun_Touch;
}


/*
 ==============================================================================

 WEAPON_NAILGUN

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponNailgun_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponNailgun_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/nailgun");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponNailgun_Touch;
}


/*
 ==============================================================================

 WEAPON_SUPERNAILGUN

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponSuperNailgun_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponSuperNailgun_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/superNailgun");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponSuperNailgun_Touch;
}


/*
 ==============================================================================

 WEAPON_GRENADELAUNCHER

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponGrenadeLauncher_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponGrenadeLauncher_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/grenadeLauncher");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponGrenadeLauncher_Touch;
}


/*
 ==============================================================================

 WEAPON_ROCKETLAUNCHER

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponRocketLauncher_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponRocketLauncher_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/rocketLauncher");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponRocketLauncher_Touch;
}


/*
 ==============================================================================

 WEAPON_LIGHTNING

 ==============================================================================
*/


/*
 ==================
 
 ==================
*/
static void SG_WeaponLightning_Touch (etEntity *self, etEntity *other, const trace_t *trace){

}

/*
 ==================
 
 ==================
*/
void SG_WeaponLightning_Spawn (etEntity *self){

	item_t	*item = (item_t *)self->state.buffer;

	// Parse the spawn parameters that will be transmitted to clients
	self->ParseItemState();

	// Get the audible range
	self->audibleRange = DEFAULT_AUDIBLERANGE;
	self->audibleRange = Max(self->audibleRange, 0.0f);

	// Get the sound shader
	self->soundShader = SG_SoundShaderIndex("weapons/lightningGun");

	// Finish setting up
	if (item->model)
		self->flags |= FL_VISIBLE;

	if (self->soundShader)
		self->flags |= FL_AUDIBLE;

	// Set the clip model

	// Enable physics
	self->EnablePhysics();

	// Set the callback functions
	self->Touch = SG_WeaponLightning_Touch;
}